﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Text;

namespace Runda.CMS.Portal.Report
{
    public partial class ARRpt_01 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sqlWhere = getWhereClause();
                LoadContractTrack(sqlWhere);

            }
        }

        private void LoadContractTrack(string sqlWhere)
        {
            //set reference
            ReportViewer ReportViewer = rvReporting;

            //get rdlc path
            string strRootPath = this.Server.MapPath(this.Request.ApplicationPath);
            string strFilePath = strRootPath + "Report\\rdlc\\ARRpt_01.rdlc";

            strFilePath = "report/rdlc/ARRpt_01.rdlc";

            //set report viewer property
            ReportViewer.SizeToReportContent = true;
            ReportViewer.ZoomMode = ZoomMode.Percent;
            ReportViewer.AsyncRendering = false;
            
            //set report viewer work mode   
            ReportViewer.ProcessingMode = ProcessingMode.Local;
            LocalReport lp = ReportViewer.LocalReport;
            lp.ReportPath = strFilePath;
            lp.EnableHyperlinks = true;
            
            //get dataset  
            BillBll bll = new BillBll();
            DataSet ds;
            ds = bll.ARRpt_01(sqlWhere);
            ds.DataSetName = "ds1_ARRpt_01";
            //set data set
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "ds1_ARRpt_01";

            DataTable dt = ds.Tables[0];
            rds.Value = dt;
            lp.DataSources.Clear();
            lp.DataSources.Add(rds);

            ReportParameter rp_Customername = new ReportParameter("rp_Customername", Request.QueryString["Customername"].ToString());
            ReportViewer.LocalReport.SetParameters(new ReportParameter[] { rp_Customername });
            ReportViewer.LocalReport.Refresh();
            
        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();

            if (Request.QueryString["CustomerCode"] != null)
            {
                sb.Append(" and cCusCode = '" + Request.QueryString["CustomerCode"].ToString() + "'");
            }
            else
            {
                sb.Append("");
            }
            
            return sb.ToString();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string sqlWhere = getWhereClause();
            LoadContractTrack(sqlWhere);
        }

        
    }
}
